﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class NewsApp : INews
    {
        private string dbname = "News";

        public void Add(NewsEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into News(");
            builder.Append("Titles,Infos,CreateTime,LinkAddress");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.Titles + "',");
            builder.Append("'" + model.Infos + "',");
            builder.Append("'" + model.CreateTime + "',");
            builder.Append("'" + model.LinkAddress + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete News ");
            builder.Append(" where id=" + id);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from News ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "id", "News", swhere);
        }

        public NewsEnt GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select id,Titles,Infos,CreateTime,LinkAddress from News ");
            builder.Append(" where id=" + id);
            NewsEnt ent = new NewsEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.id = id;
            if (table.Rows.Count > 0)
            {
                ent.Titles = table.Rows[0]["Titles"].ToString();
                ent.Infos = table.Rows[0]["Infos"].ToString();
                ent.CreateTime = table.Rows[0]["CreateTime"].ToString();
                ent.LinkAddress = table.Rows[0]["LinkAddress"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from News ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "id");
        }

        public void Update(NewsEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update News set ");
            builder.Append("Titles='" + model.Titles + "',");
            builder.Append("Infos='" + model.Infos + "',");
            builder.Append("CreateTime='" + model.CreateTime + "',");
            builder.Append("LinkAddress='" + model.LinkAddress + "'");
            builder.Append(" where id=" + model.id + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

